package com.ziq.zcamp.community

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.facebook.drawee.generic.RoundingParams
import com.ziq.base.base.BaseFragment
import com.ziq.base.extension.*
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.post.PostingDetail
import com.ziq.zcamp.databinding.FragmentSubjectListBinding
import com.ziq.zcamp.databinding.ItemSubjectContentListBinding
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.PostManager
import com.ziq.zcamp.util.ImageUtil
import com.ziq.zcamp.view.StatusView
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SubjectContentListFragment: BaseFragment(R.layout.fragment_subject_list){


    companion object {
        fun createFragment(id:Int) : SubjectContentListFragment {
            val fragment = SubjectContentListFragment()
            val bundle = Bundle()
            bundle.putInt("subject_id", id)
            fragment.arguments = bundle
            return fragment
        }
    }

    val mViewModel: SubjectContentListFragmentViewModel by viewModels()

    private val binding: FragmentSubjectListBinding? by binding()
    private var adapter: ContentListAdapter? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewModel.subjectId = arguments?.getInt("subject_id", 0) ?: 0
        binding?.root
        mViewModel.statusShow.observe(viewLifecycleOwner, Observer<Int> {
            when(it){
                StatusView.IDLE -> {
                    binding?.statusView?.setStatus(StatusView.IDLE)
                }
                StatusView.LOADING -> {
                    binding?.statusView?.setStatus(StatusView.LOADING)
                }
                StatusView.NO_DATA -> {
                    binding?.statusView?.setStatus(StatusView.NO_DATA,
                        isCenter = false,
                        marginTop = 50.dp,
                        imgSrc = R.drawable.pic_no_content_camp,
                        imgWidth = 235.dp,
                        imgHeight = 151.dp,
                        msg = "文章正在新增中～",
                        backgroundColor = Color.parseColor("#ffffff")
                    )
                }
                StatusView.NET_ERROR -> {
                    binding?.statusView?.setStatus(StatusView.NO_DATA,
                        isCenter = false,
                        marginTop = 50.dp,
                        imgSrc = R.drawable.pic_no_content_camp,
                        imgWidth = 235.dp,
                        imgHeight = 151.dp,
                        msg = "文章正在新增中～",
                        backgroundColor = Color.parseColor("#ffffff")
                    )
                }
            }
        })
        mViewModel.listRequestFinished.observe(viewLifecycleOwner, Observer<Boolean> {
            if(it){
                binding?.refreshLayout?.finishRefresh()
                binding?.refreshLayout?.finishLoadMore()
            }
        })
        mViewModel.listHasMore.observe(viewLifecycleOwner, Observer<Boolean> {
            if(it){
                binding?.refreshLayout?.resetNoMoreData()
            } else {
                binding?.refreshLayout?.setNoMoreData(true)
            }
        })

        mViewModel.list.observe(viewLifecycleOwner, Observer {
            adapter?.setData(it)
        })

        binding?.refreshLayout?.setOnRefreshListener {
            mViewModel.onRefresh(false)
        }
        binding?.refreshLayout?.setOnLoadMoreListener {
            mViewModel.onLoadMore()
        }

        binding?.recycleView?.layoutManager = LinearLayoutManager(requireContext())
        binding?.recycleView?.layoutManager = LinearLayoutManager(requireContext())
        adapter = ContentListAdapter()
        adapter?.setHasStableIds(true)
        binding?.recycleView?.adapter = adapter

        mViewModel.onRefresh(true)
    }



    class ContentListAdapter() : RecyclerView.Adapter<BindingViewHolder<ItemSubjectContentListBinding>>(){

        private var dataList : MutableList<PostingDetail>? = null
        fun setData(list: MutableList<PostingDetail>?) {
            dataList = list
            notifyDataSetChanged()
        }

        override fun getItemCount(): Int {
            return dataList?.size?:0
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BindingViewHolder<ItemSubjectContentListBinding> {
            return createBindingViewHolder(parent)
        }

        @SuppressLint("SetTextI18n")
        override fun onBindViewHolder(
            holder: BindingViewHolder<ItemSubjectContentListBinding>,
            position: Int
        ) {
            val item = dataList?.get(position)
            item?.let {
                holder.binding.root.click {
                    PostManager.navigateToDetailActivity(GlobalContextManager.appContext!!, item)
                }
                holder.binding.tvName.text = it.user?.nick_name?:""
                holder.binding.tvTitle.text = it.post?.title?:""
                ImageUtil.updateImageFromNetwork(holder.binding.ivAvatar, it.user?.header_url)
                val imageInfo = it.post?.images?.get(0)

                ImageUtil.updateImageFromNetwork(holder.binding.ivCover, imageInfo?.url)
                val roundingParams = RoundingParams.fromCornersRadii(5.dp.toFloat(), 5.dp.toFloat(), 5.dp.toFloat(),5.dp.toFloat())
                roundingParams.overlayColor = Color.WHITE
                holder.binding.ivCover.hierarchy.roundingParams = roundingParams
                if(it.post?.p_type == 2){
                    holder.binding.ivPostTypeLabel.visibility = View.VISIBLE
                    holder.binding.ivPostTypeLabel.setImageResource(R.drawable.icon_post_video_label)
                } else {
                    holder.binding.ivPostTypeLabel.visibility = View.GONE
                }
            }

        }
    }
}